package com.tools.cleanmaster.utils;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.MediaStore;

import com.tools.cleanmaster.CleanApplication;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class OptimizerFile extends File {
    private static final long serialVersionUID = 4419577344774769651L;
    private static final String EXTERNAL = "external";

    public OptimizerFile(File dir, String name) {
        super(dir, name);
    }

    public OptimizerFile(String dirPath, String name) {
        super(dirPath, name);
    }

    public OptimizerFile(String path) {
        super(path);
    }

    public OptimizerFile(URI uri) {
        super(uri);
    }

    @Override
    public boolean delete() {
        if (android.os.Build.VERSION.SDK_INT == 19) {
            return deleteByContentResolver();
        }
        return super.delete();
    }

    @SuppressLint("NewApi")
    public boolean deleteByContentResolver() {
        if (super.delete() && !exists()) {
            return true;
        }
        boolean directory = isDirectory();
        if (directory) {
            String[] files = list();
            if (files != null && files.length > 0) {
                return false;
            }
        }
        String where = MediaStore.MediaColumns.DATA + " = ?";
        String[] selectionArgs = new String[] {getAbsolutePath()};
        ContentValues value = new ContentValues();
        Uri filesUri = MediaStore.Files.getContentUri(EXTERNAL);
        ContentResolver contentResolver = CleanApplication.getInstance().getContentResolver();
        value.put(MediaStore.Files.FileColumns.MEDIA_TYPE, MediaStore.Files.FileColumns.MEDIA_TYPE_PLAYLIST);
        int updated = 0;

        try {
            updated = contentResolver.update(filesUri, value, where, selectionArgs);
        } catch (Exception e) {
            return false;
        }

        if (updated == 0) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Files.FileColumns.DATA, getAbsolutePath());
            values.put(MediaStore.Files.FileColumns.MEDIA_TYPE, 4);
            Uri filesUri2 = contentResolver.insert(filesUri, values);
            if (filesUri2 != null) {
                value.put(MediaStore.Files.FileColumns.MEDIA_TYPE,
                        MediaStore.Files.FileColumns.MEDIA_TYPE_PLAYLIST);
                updated = contentResolver.update(filesUri2, value, null, null);
                contentResolver.delete(filesUri2, null, null);
            }
        } else {
            contentResolver.delete(filesUri, where, selectionArgs);
        }
        return !exists();
    }

    @Override
    public File[] listFiles() {
        String[] filenames = list();
        if (filenames == null) {
            return null;
        }
        int count = filenames.length;
        File[] result = new OptimizerFile[count];
        for (int i = 0; i < count; ++i) {
            result[i] = new OptimizerFile(this, filenames[i]);
        }
        return result;
    }

    @Override
    public File[] listFiles(FileFilter filter) {
        File[] files = listFiles();
        if (filter == null || files == null) {
            return files;
        }
        List<File> result = new ArrayList<File>(files.length);
        for (File file : files) {
            if (filter.accept(file)) {
                result.add(file);
            }
        }
        return result.toArray(new OptimizerFile[result.size()]);
    }

    @Override
    public File getParentFile() {
        String tempParent = getParent();
        if (tempParent == null) {
            return null;
        }
        return new OptimizerFile(tempParent);
    }

    @Override
    public File[] listFiles(FilenameFilter filter) {
        return filenamesToFiles(list(filter));
    }

    private File[] filenamesToFiles(String[] filenames) {
        if (filenames == null) {
            return null;
        }
        int count = filenames.length;
        OptimizerFile[] result = new OptimizerFile[count];
        for (int i = 0; i < count; ++i) {
            result[i] = new OptimizerFile(this, filenames[i]);
        }
        return result;
    }
}
